<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/1
 * Time: 11:23
 */

namespace app\common\model;


use think\Db;
use think\Model;

class SkillDisplaym extends Model
{
    /**
     * 名片管理列表
     */
    public static function skillDisplayList($map = [], $page)
    {
        return Db::name('skill_display')->alias('sd')
            ->join('user', 'u_id = sd_uid', 'left')
            ->where($map)
            ->field('sd.*, u_nickname')
            ->order('sd_id DESC')
            ->paginate(15, false, ['page' => $page]);
    }

    /**审核名片
     * @param $id 名片ID
     * @param $status 名片状态
     * @param $uid 用户ID
     * @param sdtitle  名片标题
     * @return bool
     */
    public static function editSkillDisplay($id, $status, $uid, $sdtitle)
    {
        if($status == 3){//审核通过
            $u_integral = Db::name('systems')->where('s_id', 1)->value('s_business_card');//名片审核通过后  获得的积分
            Db::startTrans();
            try{
                //名片审核通过
                Db::name('skill_display')->where('sd_id', $id)->update(['sd_status' => $status, 'sd_adopt_time' => time()]);
                Db::name('user')->where('u_id', $uid)->setInc('u_integral',$u_integral);
                $title   = '名片审核通过';
                $content = '恭喜您，名片:《'.$sdtitle.'》审核通过。获得'.$u_integral.'积分。';
                Messagem::addUserMessage($uid, $title, $content,2);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        } elseif ($status == 4) {
            Db::startTrans();
            try{
                //名片审核不通过
                Db::name('skill_display')->where('sd_id', $id)->update(['sd_status' => $status, 'sd_not_adopt_time' => time()]);
                $title   = '名片审核失败';
                $content = '很抱歉您的名片:《'.$sdtitle.'》审核未通过，请重新提交您要发布的名片';
                Messagem::addUserMessage($uid, $title, $content, 4);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 名片的详情
     */
    public static function skillDisplayDetail($id)
    {
        return Db::name('skill_display')->alias('sd')
            ->join('user', 'u_id = sd_uid', 'left')
            ->join('area ap', 'ap.id = sd_province', 'left')
            ->join('area ac', 'ap.id = sd_city', 'left')
            ->where('sd_id', $id)
            ->field('sd.*, u_nickname, ap.name as pName, ac.name as cName')
            ->find();
    }

}